﻿using RSFrame.Services;
using RSModels;
using System;
using System.Collections.Generic;
using System.Data.Common;

namespace DriverService
{
    class DriverService : IDriverService
    {
        public IDBService DBService { get; set; }

        public DriverService(IDBService dBService)
        {
            DBService = dBService;
        }

        public List<Driver> AllDrivers
        {
            get
            {
                try
                {
                    List<Driver> result = new();
                    DbConnection c = DBService.EstablishConnection();
                    DbDataReader a = DBService.ReadData(c, "exec dbo.get_all_drivers",
                        new Dictionary<string, object> { });
                    while (a.Read())
                    {
                        result.Add(new Driver()
                        {
                            ID = Convert.ToInt32(a[0]),
                            Name = (string)a[1],
                            CompanyID = Convert.ToInt32(a[2]),
                        });
                    }
                    DBService.CloseConnection(c);
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }

        public int? AddDriver(string name, int companyID)
        {
            try
            {
                DbConnection c = DBService.EstablishConnection();
                object Result = DBService.AddData(c, "exec dbo.add_driver @companyID=@_companyID,@name=@_name", new Dictionary<string, object>
                {
                    { "@_companyID", companyID },
                    { "@_name", name },
                });
                DBService.CloseConnection(c);
                return Convert.ToInt32(Result);
            }
            catch
            {
                return null;
            }
        }

        public bool DeleteDriver(int id)
        {
            try
            {
                DbConnection c = DBService.EstablishConnection();
                int a = DBService.WriteData(c, "exec dbo.del_driver @id=@_id", new Dictionary<string, object>
                { { "@_id", id } });
                DBService.CloseConnection(c);
                return a == 1;
            }
            catch
            {
                return false;
            }
        }

        public bool EditDriver(int ID)
        {
            throw new NotImplementedException();
        }

        public Driver GetDriverByID(int id)
        {
            try
            {
                Driver result = null;
                DbConnection c = DBService.EstablishConnection();
                DbDataReader a = DBService.ReadData(c, "exec dbo.get_driver_by_id @id=@_id",
                    new Dictionary<string, object> { { "@_id", id } });
                while (a.Read())
                {
                    result = new Driver()
                    {
                        ID = Convert.ToInt32(a[0]),
                        Name = (string)a[1],
                        CompanyID = Convert.ToInt32(a[2]),
                    };
                }
                DBService.CloseConnection(c);
                return result;
            }
            catch
            {
                return null;
            }
        }

        public List<Driver> GetDriversByCompanyID(int companyID)
        {
            try
            {
                List<Driver> temp = AllDrivers;
                List<Driver> result = new();
                if (temp != null)
                {
                    foreach (var item in temp)
                    {
                        if (item.CompanyID == companyID)
                            result.Add(item);
                    }
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
    }
}
